package com.yunduansing.demo.service.impl;

import com.yunduansing.demo.entity.PostEntity;
import com.yunduansing.demo.exception.RecordExistsException;
import com.yunduansing.demo.mapper.PostMapper;
import com.yunduansing.demo.model.common.PageRequestModel;
import com.yunduansing.demo.model.common.PageResultModel;
import com.yunduansing.demo.model.post.PostTreeModel;
import com.yunduansing.demo.service.PostService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yunduansing
 * @since 2020-05-24
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, PostEntity> implements PostService {

    @Override
    public PageResultModel<PostEntity> getPage(PageRequestModel model) {
        var wrapper = new QueryWrapper<PostEntity>();
        if (!StringUtils.isEmpty(model.getKey())) {
            wrapper.or().like("name", model.getKey()).or().like("code", model.getKey());
        }
        Page<PostEntity> page = new Page<>(model.getCurrent(), model.getPageSize());
        var data = page(page);
        return new PageResultModel<PostEntity>(data.getTotal(), data.getRecords());
    }

    @Override
    public boolean create(PostEntity entity) throws RecordExistsException {
        var exists = existsByNameCode(entity.getName(), entity.getCode());
        if (exists.stream().filter(x -> x.getCode().equals(entity.getCode())).count() > 0)
            throw new RecordExistsException("已存在code为：" + entity.getCode() + "的岗位。");
        if (exists.stream().filter(x -> x.getName().equals(entity.getName())).count() > 0)
            throw new RecordExistsException("已存在名为为：" + entity.getName() + "的岗位。");
        return save(entity);
    }

    @Override
    public boolean update(PostEntity entity) throws RecordExistsException {
        var exists = existsByNameCode(entity.getName(), entity.getCode());
        if (exists.stream().filter(x -> x.getCode().equals(entity.getCode()) && x.getId() != entity.getId())
                .count() > 0)
            throw new RecordExistsException("已存在code为：" + entity.getCode() + "的岗位。");
        if (exists.stream().filter(x -> x.getName().equals(entity.getName()) && x.getId() != entity.getId())
                .count() > 0)
            throw new RecordExistsException("已存在名为为：" + entity.getName() + "的岗位。");
        return updateById(entity);
    }

    private List<PostEntity> existsByNameCode(String name, String code) {
        var wrapper = new QueryWrapper<PostEntity>();
        wrapper.or().like("code", code).or().like("name", name);
        return list(wrapper);
    }

    @Override
    public List<PostTreeModel> getTree() {
        return generateTree(0, this.list());
    }

    private List<PostTreeModel> generateTree(int pid, List<PostEntity> entities) {
        var data = entities.stream().filter(x -> x.getPid() == pid).collect(Collectors.toList());
        Collections.sort(data);
        var list = new ArrayList<PostTreeModel>();
        if (data.size() > 0) {
            for (var item : data) {
                if (item.getStatus()!=2) {
                    list.add(new PostTreeModel(item, generateTree(item.getId(), entities)));
                }
            }
        }
        return list;
    }

}
